//
//  ColorButton.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/11/12.
//

import SwiftUI

struct ColorButton: View {
    var isChecked: Bool
    var color: String
    var action: () -> Void
    
    var body: some View {
        Button(action: {
            action()
        }, label: {
            ZStack {
                Circle()
                    .foregroundStyle(.accent)
                    .frame(width: boundsWidth * 0.11)
                
                Circle()
                    .foregroundStyle(getColor(color: color))
                    .frame(width: boundsWidth * 0.1)
                
                if isChecked {
                    Group {
                        if color == "黑色" {
                            Circle()
                                .foregroundStyle(.white)
                        } else if color == "白色" {
                            Circle()
                                .foregroundStyle(.black)
                        } else {
                            Circle()
                                .foregroundStyle(.accent)
                        }
                    }
                    .frame(width: boundsWidth * 0.08)
                    
                    Circle()
                        .foregroundStyle(getColor(color: color))
                        .frame(width: boundsWidth * 0.06)
                }
            }
        })
    }
}

struct ColorItem: View {
    var color: String
    
    var body: some View {
        Circle()
            .foregroundStyle(getColor(color: color))
            .frame(width: 25)
    }
}

func getColor(color: String) -> Color {
    switch color {
    case "黄色":
        return Color(#colorLiteral(red: 0.9741474986, green: 0.8708099127, blue: 0.4635377526, alpha: 1))
    case "黑色":
        return .black
    case "红色":
        return Color(#colorLiteral(red: 0.7298596501, green: 0.1338253617, blue: 0.1279890537, alpha: 1))
    case "绿色":
        return Color(#colorLiteral(red: 0.2037778199, green: 0.6082915068, blue: 0.4026482999, alpha: 1))
    case "米色":
        return Color(#colorLiteral(red: 0.9756988883, green: 0.9114929438, blue: 0.7018780112, alpha: 1))
    case "蓝色":
        return Color(#colorLiteral(red: 0.4376088977, green: 0.6728341579, blue: 0.8684461713, alpha: 1))
    case "棕色":
        return Color(#colorLiteral(red: 0.5653125048, green: 0.3719313443, blue: 0.2409709096, alpha: 1))
    case "灰色":
        return Color(#colorLiteral(red: 0.6313726306, green: 0.6313726306, blue: 0.6313726306, alpha: 1))
    case "白色":
        return .white
    case "粉色":
        return Color(#colorLiteral(red: 0.9568066001, green: 0.7281237841, blue: 0.8234816194, alpha: 1))
    case "橙色":
        return Color(#colorLiteral(red: 0.9397065639, green: 0.5877256393, blue: 0.3532759547, alpha: 1))
    case "中蓝":
        return Color((#colorLiteral(red: 0.2211736739, green: 0.4844535589, blue: 0.7168042064, alpha: 1)))
    case "深蓝":
        return Color(#colorLiteral(red: 0.09023747593, green: 0.1104305759, blue: 0.3674597144, alpha: 1))
    case "草绿":
        return Color(#colorLiteral(red: 0.5698654056, green: 0.584797442, blue: 0.1600063145, alpha: 1))
    case "浅绿":
        return Color(#colorLiteral(red: 0.796756804, green: 0.8568025231, blue: 0.36670959, alpha: 1))
    case "紫色":
        return Color(#colorLiteral(red: 0.6930487752, green: 0.6046406627, blue: 0.7853064537, alpha: 1))
    case "浅蓝":
        return Color(#colorLiteral(red: 0.6660937071, green: 0.8081252575, blue: 0.929469645, alpha: 1))
    default:
        return .white
    }
}

#Preview {
    ColorButton(isChecked: true, color: "黄色", action: {})
}

#Preview {
    ColorItem(color: "黄色")
}
